package org.itboys.shop.service;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.oss.OssService;
import org.itboys.product.constant.ProductConstant;
import org.itboys.seller.constant.SellerConstants;
import org.itboys.shop.entity.ScenicSpot;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商家
 * @author weisky
 *
 */
@Service
public class ScenicSpotService extends BaseService<ScenicSpot, Long>{

    private static final long serialVersionUID = 1L;
    @Resource(name = "sellerDS")
    private MongoDataSource ds;
    @Resource(name = "ossService")
    OssService productOssService;


    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }
    @Override
    protected Class<ScenicSpot> getEntityClass() {
        return ScenicSpot.class;
    }

    /**
     * 根据名称模糊查询列表
     * @return
     */
    public List<ScenicSpot> getShopListListByName(String sellerName) {
        return ds.createQuery(getEntityClass()).filter("name", AceUtils.getPattern(sellerName))
                .filter("isDeleted", SellerConstants.TYPE_ZERO).asList();
    }

    /**
     * 保存或修改
     * @return
     */
    public boolean insert(ScenicSpot spot, MultipartFile imgFile, MultipartFile[] imgFiles){
        try{
            //组装商品主图
            String tempFile = null;
            if(imgFile != null && !imgFile.isEmpty()){
                tempFile = productOssService.upload(imgFile.getInputStream(), ProductConstant.ImgPath.OSS_PATH);
                spot.setMainImg(ProductConstant.OSS_URL_PREFIX + ProductConstant.ImgPath.OSS_PATH + "/" + tempFile);
            }
            if(imgFiles != null && imgFiles.length > 0){
                List<String> images = Lists.newArrayListWithExpectedSize(imgFiles.length);
                for (MultipartFile file : imgFiles) {
                    if(file != null && !file.isEmpty()){
                        String image = productOssService.upload(file.getInputStream(), ProductConstant.ImgPath.OSS_PATH);
                        image = ProductConstant.OSS_URL_PREFIX + ProductConstant.ImgPath.OSS_PATH + "/" + image;
                        images.add(image);
                    }
                }
                spot.setImages(images);
            }
            if(spot.getId() != 0){//修改
                ScenicSpot oldSpot = getById(spot.getId());
                if (StringUtils.isBlank(spot.getMainImg()) && StringUtils.isNotBlank(oldSpot.getMainImg())) {
                    spot.setMainImg(oldSpot.getMainImg());
                }
                if (CollectionUtils.isEmpty(spot.getImages()) && CollectionUtils.isNotEmpty(oldSpot.getImages())) {
                    spot.setImages(oldSpot.getImages());
                }
                update(spot);
            }else{//新增
                save(spot);
            }
            return true;
        }catch(Exception e){
            e.printStackTrace();
            logger.error("SellerService.insert error", e);
            return false;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean dele(Long id){
        try{
            delete(id);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

}
